import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop_z/pages/cart_page/bloc/cart_bloc.dart';
import 'package:flutter_shop_z/shared_preferences/commodity_cart_prefs.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CartBottom extends StatelessWidget {
  double allPrice = 0; //总价格
  int allGoodsCount = 0; //商品总数量
  bool isAllCheck = true; //全选
  CartBottom(this.allGoodsCount, this.allPrice, this.isAllCheck);
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(5.0),
      color: Colors.white,
      width: ScreenUtil().setWidth(750),
      height: ScreenUtil().setHeight(120),
      child: Row(
        children: <Widget>[
          selectAllBut(context),
          allPriceArea(),
          buyButton(context),
        ],
      ),
    );
  }

//全选按钮
  Widget selectAllBut(context) {
    return Row(
      children: <Widget>[
        Checkbox(
          value: isAllCheck,
          onChanged: (bool val) {
            CommodityCartPrefs.checkAllCartState(val);
            updateCartBloc(context);
          },
          activeColor: Colors.purple,
        ),
        Text("全选"),
      ],
    );
  }

//合计
  Widget allPriceArea() {
    return Container(
      width: ScreenUtil().setWidth(430),
      alignment: Alignment.centerRight,
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                alignment: Alignment.centerRight,
                width: ScreenUtil().setWidth(280),
                child: Text(
                  '合计：',
                  style: TextStyle(fontSize: ScreenUtil().setSp(36)),
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                width: ScreenUtil().setWidth(150),
                child: Text(
                  "￥${allPrice}",
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(36), color: Colors.red),
                ),
              ),
            ],
          ),
          Container(
            width: ScreenUtil().setWidth(430),
            alignment: Alignment.centerRight,
            child: Text(
              '满100元免配送费，预约免配送费',
              style: TextStyle(
                fontSize: ScreenUtil().setSp(22),
                color: Colors.black38,
              ),
            ),
          ),
        ],
      ),
    );
  }

//结算按钮
  Widget buyButton(context) {
    return Container(
      width: ScreenUtil().setWidth(160),
      padding: EdgeInsets.only(left: 5, right: 5),
      child: InkWell(
        onTap: () {
          CommodityCartPrefs.clearisCheckCommodityCart();
          updateCartBloc(context);
          // BlocProvider.of<CartBloc>(context).add(SetCartEvent(""));
        },
        child: Container(
          padding: EdgeInsets.all(10),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: Colors.red,
            borderRadius: BorderRadius.circular(3),
          ),
          child: Text(
            '结算(${allGoodsCount})',
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }

  void updateCartBloc(context) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    BlocProvider.of<CartBloc>(context).add(SetCartEvent(
        sharedPreferences.getString(CommodityCartPrefs.cartListKey)));
  }
}
